package com.FH.applyFrom.bespeak_service;

import com.FH.organization.Attachment.AttachmentResource;
import com.FH.organization.department.DepartmentResource;
import com.FH.organization.operator_log.OperatorLogPullDTO;
import com.FH.organization.operator_log.OperatorLogResource;
import com.FH.organization.people.KamMemberResource;
import com.FH.organization.suggestion.SuggestionResource;
import com.FH.organization.work_flow.WorkFlowPullDTO;
import com.FH.organization.work_flow.WorkFlowResource;
import com.FH.util.AccessDB;
import com.FH.util.ResultDTO;
import com.FH.util.warnSrc;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import javax.ejb.Stateless;
import javax.inject.Inject;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2016/12/16.
 */
@Stateless
public class BespeakServiceImpl extends AccessDB implements BespeakServiceDAO {
    @Inject
    KamMemberResource kamMemberResource;
    @Inject
    OperatorLogResource operatorLogResource;
    @Inject
    DepartmentResource departmentResource;
    @Inject
    WorkFlowResource workFlowResource;
    @Inject
    AttachmentResource attachmentResource;
    @Inject
    SuggestionResource suggestionResource;

    @Override
    public ResultDTO getBespeakServiceInfo(String serach, String page, String size) {
        ResultDTO resultDTO = new ResultDTO();

        try {
            JSONObject json = new JSONObject(serach);
            String existName = "0";

            String sqlexit = "select count(1) from bespeak_service  where 1=1 ";

            String sql_w = "";//查询条件sql组合
            String sql_page = "";//分页组合

            Integer page_ok = Integer.parseInt(page);//将定义page、size的String类型转换成Integer类型
            Integer size_ok = Integer.parseInt(size);

            if (page.equals("1")) {
                sql_page = " limit 0," + size + "";
            } else {
                sql_page = " limit " + ((page_ok - 1) * size_ok + 1) + "," + size + "";
            }

            if (json.has("member_id")) {
                sql_w += " AND member_id = '" + json.getString("member_id") + "'";
            }
            if (json.has("bespeak_service_id")) {
                sql_w += " AND bespeak_service_id = '" + json.getString("bespeak_service_id") + "'";
            }

            String sql_ok = sqlexit + sql_w + sql_page;//组合好的sql语句
            existName = getReportingToOne(sql_ok);

            List<String[]> seresult = new ArrayList<String[]>();

            List<BespeakServiceShowDTO> result = new ArrayList<BespeakServiceShowDTO>();

            if (!existName.equals("0")) {
                String sqlq = "select bespeak_service_id,bespeak_org,bespeak_men,bespeak_time,bespeak_sth,member_id,fail_resource,create_time FROM bespeak_service where 1=1  " + sql_w + " ORDER BY create_time DESC" + sql_page;

                seresult = this.getReportingTo(sqlq);

                //构建返回对象
                for (int i = 0; i < seresult.size(); i++) {


                    BespeakServiceShowDTO bespeakServiceShowDTO = new BespeakServiceShowDTO();

                    bespeakServiceShowDTO.setBespeak_service_id(seresult.get(i)[1]);
                    bespeakServiceShowDTO.setBespeak_org(seresult.get(i)[2]);
                    bespeakServiceShowDTO.setBespeak_men(seresult.get(i)[3]);
                    bespeakServiceShowDTO.setBespeak_time(seresult.get(i)[4]);
                    bespeakServiceShowDTO.setBespeak_sth(seresult.get(i)[5]);
                    bespeakServiceShowDTO.setMember_id(seresult.get(i)[6]);
                    bespeakServiceShowDTO.setFail_resource(seresult.get(i)[7]);
                    bespeakServiceShowDTO.setCreate_time(seresult.get(i)[8]);
                    ResultDTO  worklist=null,applymen = null, bespeakmen = null,suggestion =null;

                    if(json.has("isverify")){
                        String state=json.getString("isverify");
                        worklist=workFlowResource.getWorkFlowInfo("{work_flow_formid:"+seresult.get(i)[1]+",work_flow_state:"+state+"}","1","20");
                    }else{
                        worklist=workFlowResource.getWorkFlowInfo("{work_flow_formid:"+seresult.get(i)[1]+"}","1","20");
                    }
                    bespeakServiceShowDTO.setWorkflow(worklist.getValues());

                    if (null != seresult.get(i)[3] && !"".equals(seresult.get(i)[3])) {
                        applymen = kamMemberResource.getUserListInfo("{member_id:" + seresult.get(i)[3] + "}", "1", "1");
                        bespeakServiceShowDTO.setApplymen(applymen.getValues());
                    }
                    /**
                     * 处理意见
                     */
                    if(seresult.get(i)[1] != null && ! seresult.get(i)[1].equals("")){

                        suggestion=suggestionResource.getSuggestionInfo("{form_id:" + seresult.get(i)[1] + "}", "1", "100");
                        bespeakServiceShowDTO.setSuggestionShowDTOs(suggestion.getValues());
                    }
                    if (null != seresult.get(i)[6] && !"".equals(seresult.get(i)[6])) {
                        bespeakmen = kamMemberResource.getUserListInfo("{member_id:" + seresult.get(i)[6] + "}", "1", "1");
                        bespeakServiceShowDTO.setBespeakmenlist(bespeakmen.getValues());
                    }
//                    if (null != seresult.get(i)[4] && !"".equals(seresult.get(i)[4])) {
//                        organization = departmentResource.getDepartmentInfo("{organization_id:" + seresult.get(i)[4] + "}", "1", "1");
//                        textRcorderShowDTO.setDepartments(organization.getValues());
//                    }
                    result.add(bespeakServiceShowDTO);
                }
            }
            if (seresult.size() != 0) {
                resultDTO.setStr(result.size() + "");
                resultDTO.setStatus("90000");
                resultDTO.setDescritpion(warnSrc.get_success);
            } else {
                resultDTO.setStatus("90001");
                resultDTO.setDescritpion(warnSrc.get_fail);
            }
            resultDTO.setValues(result);

        } catch (JSONException e) {
            e.printStackTrace();

        }
        return resultDTO;
    }

    @Override
    public ResultDTO addBespeakServiceInfo(BespeakServicePullDTO bespeakServicePullDTO) {
        ResultDTO resultDTO = new ResultDTO();//构建返回对象


        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = df.format(new Date());

        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");

        String existName = "";
        String bespeak_service_id = bespeakServicePullDTO.getBespeak_service_id();
        if (bespeak_service_id != null && !bespeak_service_id.equals("")) {

            String sql = "select count(1) from bespeak_service where bespeak_service_id='" + bespeak_service_id + "'";

            existName = getReportingToOne(sql);

            if (!existName.equals("0")) {
                return updateBespeakServiceInfo(bespeakServicePullDTO);
            }
        }

        String QUERY = "";

        //表中查看时间设为空值
        QUERY += "insert into bespeak_service(bespeak_service_id,bespeak_org,bespeak_men,bespeak_time,bespeak_sth,member_id,create_time,isverify) values" +
                " ('" + uuid + "','" + bespeakServicePullDTO.getBespeak_org() + "','" + bespeakServicePullDTO.getBespeak_men()
                + "','" + bespeakServicePullDTO.getBespeak_time() + "','" + bespeakServicePullDTO.getBespeak_sth() + "','" + bespeakServicePullDTO.getMember_id() + "','" + time + "','0')";
        int result = this.addReportingTo(QUERY);
        // 判断SQl是否执行成功
        if (result != 0) {
            resultDTO.setStr(uuid);
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.add_success);
            //日志操作
            OperatorLogPullDTO operatorlogPullDTO = new OperatorLogPullDTO();
            operatorlogPullDTO.setWork_form_id(uuid);
            operatorlogPullDTO.setOperator_log_type("服务预约");
            operatorlogPullDTO.setOperator_log_title("服务预约");
            operatorlogPullDTO.setOperator_log_content(bespeakServicePullDTO.getBespeak_sth());
            operatorlogPullDTO.setMember_id(bespeakServicePullDTO.getMember_id());
            operatorLogResource.addOperatorLogInfo(operatorlogPullDTO);

            //流程上报

            WorkFlowPullDTO workFlowPullDTO = new WorkFlowPullDTO();
            workFlowPullDTO.setWork_flow_formid(uuid);
            workFlowPullDTO.setWork_flow_type("bespeak_service");
            workFlowPullDTO.setWork_flow_desc("服务预约请求审批");
            workFlowPullDTO.setWork_flow_date(time);
            workFlowPullDTO.setWork_flow_order("0");
            workFlowPullDTO.setWork_flow_createid(bespeakServicePullDTO.getMember_id());
            workFlowPullDTO.setWork_flow_approval(bespeakServicePullDTO.getReport_people());
            workFlowPullDTO.setWork_flow_approval_organization_id(bespeakServicePullDTO.getWork_flow_approval_organization_id());
            workFlowPullDTO.setFlow_path_id("");
            workFlowPullDTO.setWork_flow_suggestion("");
            workFlowResource.addWorkFlowInfo(workFlowPullDTO);
        } else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.add_fail);
        }
        return resultDTO;
    }


    @Override
    public ResultDTO updateBespeakServiceInfo(BespeakServicePullDTO bespeakServicePullDTO) {
        ResultDTO resultDTO = new ResultDTO();

        String QUERY = "";

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = df.format(new Date());

        //更新sql语句
        QUERY += "update bespeak_service set create_time ='" + time + "'";

        if (null != bespeakServicePullDTO.getBespeak_org() && !"".equals(bespeakServicePullDTO.getBespeak_org())) {
            QUERY += ",bespeak_org ='" + bespeakServicePullDTO.getBespeak_org() + "'";
        }
        if (null != bespeakServicePullDTO.getBespeak_men() && !"".equals(bespeakServicePullDTO.getBespeak_men())) {
            QUERY += ",bespeak_men ='" + bespeakServicePullDTO.getBespeak_men() + "'";
        }
        if (null != bespeakServicePullDTO.getBespeak_time() && !"".equals(bespeakServicePullDTO.getBespeak_time())) {
            QUERY += ",bespeak_time ='" + bespeakServicePullDTO.getBespeak_time() + "'";
        }
        if (null != bespeakServicePullDTO.getBespeak_time() && !"".equals(bespeakServicePullDTO.getBespeak_time())) {
            QUERY += ",bespeak_time ='" + bespeakServicePullDTO.getBespeak_time() + "'";
        }
        if (null != bespeakServicePullDTO.getBespeak_sth() && !"".equals(bespeakServicePullDTO.getBespeak_sth())) {
            QUERY += ",bespeak_sth ='" + bespeakServicePullDTO.getBespeak_sth() + "'";
        }
        QUERY += " where bespeak_service_id = '" + bespeakServicePullDTO.getBespeak_service_id() + "'";
        int result = this.addReportingTo(QUERY);
        // 判断SQl是否执行成功
        if (result != 0) {
            resultDTO.setStr(bespeakServicePullDTO.getBespeak_service_id());
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.update_success);
        } else {
            resultDTO.setStatus("90001");
        }
        return resultDTO;
    }

    @Override
    public ResultDTO deleteBespeakServiceInfo(BespeakServicePullDTO bespeakServicePullDTO) {
        //删除之前查询是否有记录
        String sql = "select count(1) from bespeak_service where bespeak_service_id ='" + bespeakServicePullDTO.getBespeak_service_id() + "'";

        String existName = getReportingToOne(sql);

        int result = 0;

        ResultDTO resultDTO = new ResultDTO();

        if (!existName.equals("0")) {

            String QUERY = "";
            QUERY += "delete from bespeak_service where bespeak_service_id='" + bespeakServicePullDTO.getBespeak_service_id() + "'";

            result = this.addReportingTo(QUERY);
        }

        if (result != 0) {
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.delete_success);
        } else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.delete_fail);
        }
        return resultDTO;
    }
}
